Array::unique = ()->
  output = {}
  output[@[key]] = @[key] for key in [0...@length]
  value for key, value of output

String::capitalize = () ->
  @charAt(0).toUpperCase() + this[1..]


# update search options value in grid model
updateSearchOptionsValue = (url, colModel)->
  searchProps = $(colModel).map((idx, elem)->elem.name if elem.search).get()
  postData = {prop: searchProps.join(", ")}
#  console.log("url=" + url, ", postData=" + JSON.stringify(postData));

  $.ajax(url, type: 'POST',async: false, data: postData).done((json, textStatus, jqXHR)->
#    console.log("json=" + JSON.stringify(json));
    for model in colModel when model.search
      model.searchoptions =
        dataEvents: [
          {
            type: 'change'
            fn: ((e)->
#            console.log(e)
            )
          }
        ] if not model.searchoptions
      optionsValue = $(json).map((idx, elem)->
        value = elem[model.name]
        "#{value}:#{value}").get().unique().join(';')
      optionsValue = ":All;" + optionsValue if optionsValue
#      console.log("options value=" + optionsValue);
      model.searchoptions = {} unless model.searchoptions
      model.searchoptions.value = optionsValue
  )


#reload grid
$.jgrid.extend {
  getId: ()->
    "##{@prop 'id'}"

  reloadAll: (url = this.getGridParam('url'), postData = this.getGridParam('postData'))->
    gridParam = @getGridParam()
    $(gridParam.colModel).each (index, colModel)->
      colModel.classes = 'editable-column' if colModel.editable

    @GridUnload @getId()
    gridParam.url = url
    gridParam.postData = postData if postData

    updateSearchOptionsValue(url, gridParam.colModel)

    #    console.log "recreate grid, gridpostData="
    #    console.log  gridParam.postData
    delete gridParam.selarrrow
    #    delete gridParam.selrow
    newGrid = $(@getId()).jqGrid gridParam
    #    save search tool bar status before recreate the grid
    #    console.log $("#transGrid").jqGrid 'getGridParam', 'searchvalue'
    @getGridParam('afterCreate')? (newGrid)
    newGrid
}

newOption = (text, value, selected)->
  "<option #{if selected then 'selected ' else ''}value='#{value}'>#{text}</option>"


###
convert a json array to a list of options.
@params
json: json array
selectedValue: default selected option value
###
json2Options = (json, selectedValue = ':last', textFieldName = "version", valueFieldName = "id", sep = '\n')->
  $(json).map(
    (index, elem)->
      #        selected = if !selectedValue then index == json.length - 1 else (String selectedValue) == (String @[valueFieldName])
      # :last indicate that the last option need to be selected
      return false unless jQuery.isArray(json)
      selectedValue = json.slice(-1)[0][valueFieldName] if ':last' == selectedValue
      selectedValue = json.slice(0)[0][valueFieldName] if ':first' == selectedValue
      selected = (String selectedValue) == (String @[valueFieldName])
      newOption @[textFieldName], @[valueFieldName], selected
  ).get().join(sep)